//
//  UIViewController+specialNav.m
//  SkyworthPalmExam
//
//  Created by Arvin Wang on 15/1/21.
//  Copyright (c) 2015年 Arvin Wang. All rights reserved.
//

#import "UIViewController+specialNav.h"

@implementation UIViewController (specialNav)


/**
 *  导航栏标题
 */

- (void)createNavigationItemTitle:(NSString *)title
{
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0,0.0,200.0,20.0)];
    titleView.backgroundColor = [UIColor clearColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0.0,200.0,20.0)];
    titleLabel.tag = 101;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:20.0f];
    [titleView addSubview:titleLabel];
    
    self.navigationItem.titleView = titleView;
    
}


/**
 *  导航栏左边按钮
 */
- (void)createLeftButtonWithTitle:(NSString *)title withTitleColor:(UIColor *)titleColor
                    withLeftImage:(NSString *)leftImageName selector:(SEL)selector
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0f, 40.0f, 44.0)];
    [leftButton setBackgroundColor:[UIColor clearColor]];
    if (title && [title length] > 0) {
        leftButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        leftButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [leftButton setTitleColor:titleColor forState:UIControlStateNormal];
        [leftButton setTitle:title forState:UIControlStateNormal];
        [leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0f, iOS7Later ?-10.0f : 7.0f, 0.0f, 0.0f)];
    }
    
    if (leftImageName && [leftImageName length] > 0) {
        
        [leftButton setImage:[UIImage imageNamed:leftImageName] forState:UIControlStateNormal];
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0.0f, iOS7Later ? -20.0f : -3.0f, 0.0f, 0.0f)];
    }
    [leftButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barItem;

}

/**
 * 导航栏右边按钮
 */
- (void)createRightButtonWithTitle:(NSString *)title withTitleColor:(UIColor *)titleColor
                    withRightImage:(NSString *)rightImageName selector:(SEL)selector
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setBackgroundColor:[UIColor clearColor]];
    if (title && [title length] > 0) {
        rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
        rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [rightButton setTitle:title forState:UIControlStateNormal];
        [rightButton setTitleColor:titleColor forState:UIControlStateNormal];
        CGSize size =  [KwSize boundingRectWithText:title Font:[UIFont systemFontOfSize:17.0]];
        rightButton.frame = CGRectMake(0, 0, size.width <= 10 ? 70 : size.width + 10, 44);
        
       // [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f, iOS7Later ?-30.0f : 7.0f)];
    }
    
    if (rightImageName && [rightImageName length]) {
        UIImage *image = [UIImage imageNamed:rightImageName];
        rightButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        [rightButton setImage:image forState:UIControlStateNormal];
        [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, -15)];
    }
    [rightButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barItem;

}

@end
